﻿using ModuleBase.googoltech.GTS;
using ModuleBase;
using System.Diagnostics;
using System.Collections;
using System.Windows.Documents;
using Prism.Ioc;
using Newtonsoft.Json;

namespace TakingTheMold
{
    public class TakingTheMold : ModuleBase.ModuleBase
    {
        private IAxisCardService _axisCardService;
        private ModuleStateService _moduleStateService;
        private IContainerProvider _containerProvider;
        bool outputs = false;
        Stopwatch sw = new Stopwatch();

        private object _parameter1;
        public object Parameter1
        {
            get => _parameter1;
            set
            {
                if (_parameter1 != value)
                {
                    _parameter1 = value;
                    OnPropertyChanged(nameof(Parameter1));
                }
            }
        }
        private object _parameter2;
        public object Parameter2
        {
            get => _parameter2;
            set
            {
                if (_parameter2 != value)
                {
                    _parameter2 = value;
                    OnPropertyChanged(nameof(Parameter2));
                }
            }
        }
        private object _parameter3;
        public object Parameter3
        {
            get => _parameter3;
            set
            {
                if (_parameter3 != value)
                {
                    _parameter3 = value;
                    OnPropertyChanged(nameof(Parameter3));
                }
            }
        }
        private object _parameter4;
        public object Parameter4
        {
            get => _parameter4;
            set
            {
                if (_parameter4 != value)
                {
                    _parameter4 = value;
                    OnPropertyChanged(nameof(Parameter4));
                }
            }
        }
        private double _parameter5;
        public double Parameter5
        {
            get => _parameter5;
            set
            {
                if (_parameter5 != value)
                {
                    _parameter5 = value;
                    OnPropertyChanged(nameof(Parameter5));
                }
            }
        }
        private double _parameter6;
        public double Parameter6
        {
            get => _parameter6;
            set
            {
                if (_parameter6 != value)
                {
                    _parameter6 = value;
                    OnPropertyChanged(nameof(Parameter6));
                }
            }
        }
        private double _parameter7;
        public double Parameter7
        {
            get => _parameter7;
            set
            {
                if (_parameter7 != value)
                {
                    _parameter7 = value;
                    OnPropertyChanged(nameof(Parameter7));
                }
            }
        }
        public override Dictionary<string, object> GetProperties()
        {
            return new Dictionary<string, object>
            {
                { "位置", Parameter1 },
                { "Y轴", Parameter2 },
                { "Z轴", Parameter3 },
                { "R轴", Parameter4 },
                { "Y速度", Parameter5 },
                { "Z速度", Parameter6 },
                { "R速度", Parameter7 },
            };
        }

        public override Dictionary<string, List<object>> PropertyOptions { get; } = new Dictionary<string, List<object>>
        {
            { "位置", new List<object> {  "左","右" } },
            { "Y轴", new List<object> {  1,2,3,4,5,6,7,8 } },
            { "Z轴", new List<object> {  1,2,3,4,5,6,7,8 } },
            { "R轴", new List<object> {  1,2,3,4,5,6,7,8 } }
        };

        public TakingTheMold(IAxisCardService axisCardService, ModuleStateService moduleStateService, IContainerProvider containerProvider)
        {
            _containerProvider = containerProvider;
            _axisCardService = axisCardService;
            _moduleStateService = moduleStateService;
            _moduleStateService.RegisterModule(Id);
            Name = "取模";

            DisplayNameToPropertyNameMap = new Dictionary<string, string>
            {
                { "位置", nameof(Parameter1) },
                { "Y轴", nameof(Parameter2) },
                { "Z轴", nameof(Parameter3) },
                { "R轴", nameof(Parameter4) },
                { "Y速度", nameof(Parameter5) },
                { "Z速度", nameof(Parameter6) },
                { "R速度", nameof(Parameter7) }
            };
        }

        public override bool Execute()
        {
            Status = ModuleStatus.Executing; // 设置状态为执行中
            int exoIndex = 7;
            AxisParm _axisY = GetAxisByNumber((int)Parameter2);
            AxisParm _axisZ = GetAxisByNumber((int)Parameter3);
            AxisParm _axisR = GetAxisByNumber((int)Parameter3);
            var takeMoldPositionY = 0.0;
            var takeMoldPositionZ = 0.0;
            var takeMoldPositionR = 0.0;
            var photoPositionZ = 0.0;
            var photoPositionR = 0.0;
            using var mdb = _containerProvider.Resolve<MDbContext>();
            if (Parameter1.ToString() == "左")
            {
                var _mParam = mdb.MParams.FirstOrDefault(x => x.Name == "TakeMoldPosition" && x.Hand == 0);
                if (_mParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_mParam.Value);
                    takeMoldPositionY = _point.Y;
                    takeMoldPositionZ = _point.Z;
                    takeMoldPositionR = _point.R;
                }
                _mParam = mdb.MParams.FirstOrDefault(x => x.Name == "PhotoMoldPosition" && x.Hand == 0);
                if (_mParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_mParam.Value);
                    photoPositionZ = _point.Z;
                    photoPositionR = _point.R;
                }
                exoIndex = 7;
            }
            else
            {
                var _mParam = mdb.MParams.FirstOrDefault(x => x.Name == "TakeMoldPosition" && x.Hand == 1);
                if (_mParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_mParam.Value);
                    takeMoldPositionY = _point.Y;
                    takeMoldPositionZ = _point.Z;
                    takeMoldPositionR = _point.R;
                }
                _mParam = mdb.MParams.FirstOrDefault(x => x.Name == "PhotoMoldPosition" && x.Hand == 1);
                if (_mParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_mParam.Value);
                    photoPositionZ = _point.Z;
                    photoPositionR = _point.R;
                }
                exoIndex = 9;
            }
            try
            {
                int stepNum = 1;
                while (true)
                {
                    switch (stepNum)
                    {
                        case 1:
                            if (!_moduleStateService.IsStopped)
                            {
                                GTSCard.AxisPosMove(ref _axisY, takeMoldPositionY, (double)Parameter5);
                                stepNum = 4;
                            }
                            break;
                        case 4: //Y轴运行到吸嘴取膜位
                            if (!_moduleStateService.IsStopped)
                            {
                                if (GTSCard.AxisPosMoveCheckDone(_axisY))
                                {
                                    stepNum = 5;
                                }
                            }
                            else
                            {
                                GTSCard.AxisStop(_axisY, 0);
                                stepNum = 1;
                            }
                            break;
                        case 5:
                            if (!_moduleStateService.IsStopped)
                            {
                                GTSCard.AxisPosMove(ref _axisZ, takeMoldPositionZ, (double)Parameter6);
                                stepNum = 6;
                            }
                            break;
                        case 6: //Z轴运行到吸嘴取膜位
                            if (!_moduleStateService.IsStopped)
                            {
                                if (GTSCard.AxisPosMoveCheckDone(_axisZ))
                                {
                                    stepNum = 7;
                                }
                            }
                            else
                            {
                                GTSCard.AxisStop(_axisZ, 0);
                                stepNum = 5;
                            }
                            break;
                        case 7: //打开真空吸，等待0.5s
                            if (!_moduleStateService.IsStopped)
                            {
                                GTSCard.SetDo((short)_axisY.CardNo, (short)exoIndex, 1);
                                sw.Start();
                                stepNum = 8;
                            }
                            break;
                        case 8:
                            if (!_moduleStateService.IsStopped)
                            {
                                if (sw.Elapsed.TotalSeconds > 0.5)
                                {
                                    sw.Stop();
                                    stepNum = 9;
                                }
                            }
                            break;
                        case 9://吸嘴Z轴运行到拍照位
                            if (!_moduleStateService.IsStopped)
                            {
                                GTSCard.AxisPosMove(ref _axisZ, photoPositionZ, (double)Parameter6);
                                stepNum = 10;
                            }
                            break;
                        case 10:
                            if (!_moduleStateService.IsStopped)
                            {
                                if (GTSCard.AxisPosMoveCheckDone(_axisZ))
                                {
                                    stepNum = 11;
                                }
                            }
                            else
                            {
                                GTSCard.AxisStop(_axisZ, 0);
                                stepNum = 9;
                            }
                            break;
                        case 11://R轴运行到拍照位
                            if (!_moduleStateService.IsStopped)
                            {
                                GTSCard.AxisPosMove(ref _axisR, photoPositionR, (double)Parameter7);
                                stepNum = 10;
                            }
                            break;
                        case 12:
                            if (!_moduleStateService.IsStopped)
                            {
                                if (GTSCard.AxisPosMoveCheckDone(_axisR))
                                {
                                    stepNum = 13;
                                }
                            }
                            else
                            {
                                GTSCard.AxisStop(_axisR, 0);
                                stepNum = 11;
                            }
                            break;
                        case 13:
                            outputs = true;
                            return outputs;
                    }
                }
            }
            catch (Exception exp)
            {
                outputs = false;
            }
            return outputs;
        }

        public override async Task<bool> ExecuteAsync()
        {
            var result = await Task.Run(() => Execute());
            return result;
        }

        public override void Initialize()
        {

        }

        private AxisParm GetAxisByNumber(int axisNumber)
        {
            switch (axisNumber)
            {
                case 1: return _axisCardService.A1;
                case 2: return _axisCardService.A2;
                case 3: return _axisCardService.A3;
                case 4: return _axisCardService.A4;
                case 5: return _axisCardService.A5;
                case 6: return _axisCardService.A6;
                case 7: return _axisCardService.A7;
                case 8: return _axisCardService.A8;
                default: return _axisCardService.A1;
            }
        }
    }
}
